package com.gxa.exchangerate.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.R;
import com.gxa.common.entity.financial_management.ExchangeRateManagement;
import com.gxa.common.entity.financial_management.ExchangeRateRecord;
import com.gxa.exchangerate.mapper.ExchangeRateMapper;
import com.gxa.exchangerate.mapper.ExchangeRateRecordMapper;
import com.gxa.exchangerate.service.ExchangeRateService;
import com.gxa.exchangerate.utils.DateUtils;
import com.gxa.exchangerate.utils.Huilv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ExchangeRateServiceImpl extends ServiceImpl<ExchangeRateMapper, ExchangeRateManagement> implements ExchangeRateService {

    @Autowired
    private ExchangeRateMapper exchangeRateMapper;

    @Autowired
    private ExchangeRateRecordMapper exchangeRateRecordMapper;

    //分页
    @Cacheable(value = "exchangeRateList",key = "'selectExchangeRateList'")
    @Override
    public R selectExchangeRateList(Integer currentPage, Integer pageSize) {
        Page page = new Page(currentPage,pageSize);
        Page selectPage = exchangeRateMapper.selectPage(page, null);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map exchangeRateList = new HashMap();
            exchangeRateList.put("totalSize",selectPage.getTotal());
            exchangeRateList.put("totalPage",selectPage.getPages());
            exchangeRateList.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(exchangeRateList);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    //条件查询
    @Override
    public R selectExchangeRate(Integer currentPage, Integer pageSize, ExchangeRateManagement exchangeRateManagement) {
        Page page = new Page(currentPage,pageSize);
        QueryWrapper qw = new QueryWrapper();
        qw.eq(exchangeRateManagement.getBaseCurrency() != null,"status",exchangeRateManagement.getBaseCurrency())
                .between(exchangeRateManagement.getStartTime() != null&&exchangeRateManagement.getEndTime() != null,"end_valid_time",exchangeRateManagement.getStartTime(),exchangeRateManagement.getEndTime())
                .lt(exchangeRateManagement.getStartTime() == null && exchangeRateManagement.getEndTime() != null,"end_valid_time",exchangeRateManagement.getEndTime());
        Page selectPage = exchangeRateMapper.selectPage(page, qw);
        R r = new R();
        if (selectPage.getRecords() != null) {
            Map exchangeRateList = new HashMap();
            exchangeRateList.put("totalSize",selectPage.getTotal());
            exchangeRateList.put("totalPage",selectPage.getPages());
            exchangeRateList.put("totalData",selectPage.getRecords());
            r.setCode(200);
            r.setMessage("查询成功");
            r.setData(exchangeRateList);
        }else {
            r.setCode(500);
            r.setMessage("查询失败");
        }
        return r;
    }

    //修改浮动红线
    @Override
    public R updateExchangeRateRedLine(ExchangeRateManagement exchangeRateManagement) {
        UpdateWrapper<ExchangeRateManagement> uw = new UpdateWrapper<>();
        uw.eq("status",0).set("floating_red_line",exchangeRateManagement.getFloatingRedLine());
        int update = exchangeRateMapper.update(null, uw);
        R r = new R();
        if (update > 0) {
            r.setCode(200);
            r.setMessage("修改成功");
        }else {
            r.setCode(500);
            r.setMessage("修改失败");
        }
        return null;
    }


    //查询银行汇率并添加到数据库
    @Override
    public void selectExchangeRateRecord() {
        JSONArray jsonArray = Huilv.run();
        int result = 0;
        for (int i = 0; i < jsonArray.size(); i++) {
            ExchangeRateRecord exchangeRateRecord = new ExchangeRateRecord();
            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
            if ("美元".equals(jsonObject1.getString("coin_name"))||"菲律宾比索".equals(jsonObject1.getString("coin_name"))){
                String spot_buy = jsonObject1.getString("spot_buy");//现汇买入价
                String spot_sell = jsonObject1.getString("spot_sell");//现汇卖出价
                exchangeRateRecord.setTradeCurrency(jsonObject1.getString("coin_name"));
                exchangeRateRecord.setPurchasePrice(new BigDecimal(spot_buy));
                exchangeRateRecord.setSellPrice(new BigDecimal(spot_sell));
                exchangeRateRecord.setReleaseTime(new Date());
                result += exchangeRateRecordMapper.insert(exchangeRateRecord);
            }
        }
    }

    //修改基准汇率
    public R updateExchangeRate(){
        String startTime = DateUtils.addDayToDate(-1, new Date(), "yyyy-MM-dd 00:00:00");
        QueryWrapper qw = new QueryWrapper();
        qw.eq("start_valid_time",startTime);
        List<ExchangeRateManagement> exchangeRateManagements = exchangeRateMapper.selectList(qw);

        return null;
    }
}
